package com.sky.controller.user;

import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController(value = "userOrderController")
@RequestMapping(value = "/user/order")
@Api(tags = "用户下单相关接口")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;

    /*用户下单方法*/
    @PostMapping(value = "/submit")
    @ApiOperation(value = "用户下单方法")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        //记录日志
        log.info("用户下单方法,当前用户的订单信息为：{}", ordersSubmitDTO);
        return Result.success(orderService.submit(ordersSubmitDTO));
    }

    /**
     * 订单支付
     * 通过评论区方法直接跳过微信支付https://www.bilibili.com/read/cv25414453/
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        // 调用service层修改支付状态
        orderService.paySuccess(ordersPaymentDTO.getOrderNumber());
        log.info("默认支付成功,参数是订单编号{}", ordersPaymentDTO.getOrderNumber());
        return Result.success(orderPaymentVO);
    }

    /*用户历史订单查询*/
    @GetMapping("/historyOrders")
    @ApiOperation("用户历史订单查询")
    public Result<PageResult> page(int page, int pageSize, Integer status) {
        log.info("用户历史订单查询,当前页码为：{},每页记录数：{},订单状态：{}", page, pageSize, status);
        PageResult pageResult = orderService.pageQueryForUser(page, pageSize, status);
        return Result.success(pageResult);
    }

    /*历史订单详情查看*/
    @GetMapping(value = "orderDetail/{id}")
    @ApiOperation(value = "查看订单详情方法")
    public Result<OrderVO> getDetailByOrderId(@PathVariable(value = "id") Long id) {
        log.info("根据订单id查询订单详情，参数为{}", id);
        // 调用service层
        OrderVO orderVO = orderService.getDetailByOrderId(id);
        return Result.success(orderVO);
    }

    /*取消订单*/
    @PutMapping(value = "cancel/{id}")
    public Result<String> cancelOrder(@PathVariable(value = "id") Long id) throws Exception {
        log.info("根据订单id取消订单，参数为{}", id);
        // 调用service层
        orderService.userCancelById(id);
        return Result.success();
    }

    /*用户再来一单*/
    @PostMapping(value = "repetition/{id}")
    @ApiOperation(value = "用户再来一单方法")
    public Result<String> repetition(@PathVariable(value = "id") Long id) {
        /*根据订单再来一单*/
        // 输出日志
        log.info("根据订单id再来一单，参数为{}", id);
        orderService.repetition(id);
        return Result.success();
    }

    /*用户催单功能*/
    @GetMapping(value = "/reminder/{id}")
    @ApiOperation(value = "用户催单功能")
    public Result<String> reminder(@PathVariable(value = "id") Long id) {
        log.info("用户催单功能，参数为{}", id);
        orderService.reminder(id);
        return Result.success();
    }
}
